package df.bean.process;

import java.sql.SQLException;

import df.bean.db.conn.DBConn;


public class ProcessExportOnwardBean {
	private DBConn conn = null;

	public boolean processOnward(String hospitalCode, String yyyy, String mm) {
		boolean result = false;
		this.conn = new DBConn();
		try {
			this.conn.setStatement();
			System.out.println("After Inser Onward");
			this.conn.insert(this.getCommandInsert(hospitalCode, yyyy, mm));
            System.out.println("Insert Onward Close");
			this.conn.insert(this.getCommandDelete(hospitalCode, yyyy, mm));
            System.out.println("Delete Onward Close");
			this.conn.commitDB();
		} catch (SQLException e) {
			this.conn .rollDB();
			System.out.println("Error Onward = " + e.getMessage());
		}finally{
			if(this.conn != null){
				this.conn.closeDB("");
			}
		}
		return result;
	}
	
	private String getCommandInsert(String hospitalCode, String yyyy, String mm) {
		String commandInsert = "";
		commandInsert = "INSERT INTO TRN_ONWARD ";
		commandInsert += "SELECT * FROM TRN_DAILY ";
		commandInsert += "WHERE IS_ONWARD = 'Y' AND TRANSACTION_DATE LIKE '"+(yyyy+mm)+"%' AND HOSPITAL_CODE = '"+hospitalCode+"' ";
		commandInsert += "AND YYYY = '' AND GUARANTEE_PAID_AMT = 0";
		return commandInsert;
	}
	
	private String getCommandDelete(String hospitalCode, String yyyy, String mm) {
		String commandDelete = "";
		commandDelete = "DELETE ";
		commandDelete += "FROM TRN_DAILY ";
		commandDelete += "WHERE IS_ONWARD = 'Y' AND TRANSACTION_DATE LIKE '"+(yyyy+mm)+"%' AND HOSPITAL_CODE = '"+hospitalCode+"' ";
		commandDelete += "AND YYYY = '' AND GUARANTEE_PAID_AMT = 0";
		return commandDelete;
	}
}
